<?php

namespace App\Http\Controllers;

use App\AdCompanies;
use App\AdList;
use App\Datatables;
use Exception;
use Illuminate\Http\Request;

/**
 * Class AdController
 * @package App\Http\Controllers
 */
class AdController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 公司列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function companiesList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::AdCompaniesList());
        }
        return view('ad.companiesList');
    }

    /**
     * 添加公司
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function companyPublish(Request $request)
    {
        if (!$request->filled('name') || !$request->filled('remarks')) {
            return response()->json(error('缺少参数'));
        }

        return response()->json(AdCompanies::CompanyPublish($request->name, $request->remarkes));
    }

    /**
     * 删除公司
     * @param AdCompanies $adCompanies
     * @return \Illuminate\Http\JsonResponse
     */
    public function companyDel(AdCompanies $adCompanies)
    {
        $adCompanies->status = AdCompanies::STATUS_OFF;
        try{
            if (!$adCompanies->save()) {
                return response()->json(error());
            }
        } catch (Exception $exception) {
            logger($exception->getMessage());
            return response()->json(error());
        }
        return response()->json(success());
    }

    /**
     * 广告列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function adList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::AdList());
        }

        return view('ad.adList');
    }

    /**
     * 添加广告
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function adPublish(Request $request)
    {
        if (!$request->department_id) {
            return response()->json(error('没有选择科室'));
        }
        return response()->json(AdList::AdPublish($request));
    }

    /**
     * 广告详情
     * @param AdList $adList
     * @return \Illuminate\Http\JsonResponse
     */
    public function adInfo(AdList $adList)
    {
        $message = $adList->toArray();
        if ($adList->department_id) {
            $message['department_id'] = explode(',', $adList->department_id);
        } else {
            $message['department_id'] = [];
        }

        return response()->json(success($message));
    }

    /**
     * 修改广告
     * @param Request $request
     * @param AdList $adList
     * @return \Illuminate\Http\JsonResponse
     */
    public function adModify(Request $request, AdList $adList)
    {
        return response()->json(AdList::AdModify($adList,$request));
    }

    public function adDel(AdList $adList)
    {
        return response()->json(AdList::AdDel($adList));
    }
}
